#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    int fd1, fd2;
    int ret;

    fd1 = open(argv[1], O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    if(-1 == fd1){
        perror("open error");
        exit(-1);
    }

    fd2 = dup2(fd1, 666);
    if(-1 == fd2){
        perror("dup error");
        ret = -1;
        goto err1;
    }

    printf("fd1: %d\nfd2:%d\n", fd1, fd2);

    err2:
        close(fd1);
        close(fd2);

    err1:
        close(fd1);
        exit(ret);

    exit(ret);
}
